﻿#region Copyright

// /*
// SilverlightInEnterprise
// SilverlightInEnterprise
// RibbonViewModel.cs
// 
// Author: Matt Van Horn
// Created 11/16/2011 2:20 AM
// Copyright: Silverlight in the Enterprise www.slinenterprise.com
// 
// Licensed under MS-PL http://www.opensource.org/licenses/MS-PL and while you 
// may use this in anyway you see fit and owe us nothing to do so, we wouldn't mind knowing that you got value out of the work we did (so we will do more) so 
// we ask that you leave a comment on www.slinenterprise.com or on the codeplex site slinenterprise.codeplex.com
// 
// */

#endregion

using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using SilverlightInEnterprise.Common.ViewModels;
using SilverlightInEnterprise.Interfaces.Ribbon;

namespace SilverlightInEnterprise.ViewModels
{
    public class RibbonViewModel : ViewModelBase, IPartImportsSatisfiedNotification
    {
        private readonly ObservableCollection<IRibbonTab> ActiveRibbonTabs = new ObservableCollection<IRibbonTab>();
        private ReadOnlyObservableCollection<IRibbonTab> _ribbonTabs;

        [ImportMany(typeof (IRibbonTab), AllowRecomposition = true)]
        internal IRibbonTab Tabs { get; set; }

        public ReadOnlyObservableCollection<IRibbonTab> RibbonTabs
        {
            get { return _ribbonTabs; }
        }

        public RibbonViewModel()
        {
            this._ribbonTabs = new ReadOnlyObservableCollection<IRibbonTab>(this.ActiveRibbonTabs);
            this.PopulateRibbon();
        }

        private void PopulateRibbon()
        {
            this.ActiveRibbonTabs.Clear();
            var potentialTabs = this.Locator.CompositionProvider.GetExports<IRibbonTab, IRibbonTabExportMetadata>();
            var actualTabs = from t in potentialTabs
                             select t;
            foreach (var tab in actualTabs)
            {
                this.ActiveRibbonTabs.Add(tab.Value);
            }
        }

        public void OnImportsSatisfied()
        {
            this.PopulateRibbon();
        }
    }
}